//https://leetcode.cn/problems/top-k-frequent-words/

class Solution {
public:
    struct greater
    {
        bool operator()(const pair<string, int>& p, const pair<string, int>& _p)
        {
            return p.second > _p.second || (p.second == _p.second && p.first < _p.first);
        }
    };
    vector<string> topKFrequent(vector<string>& words, int k) {
        map<string, int> countmap;
        for (const auto& e : words)
        {
            countmap[e]++;
        }

        vector<pair<string, int>> temp(countmap.begin(), countmap.end());
        sort(temp.begin(), temp.end(), greater());
        vector<string> ret;

        for (int i = 0; i < k; i++)
        {
            ret.push_back(temp[i].first);
        }

        return ret;

    }
};